package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Interface.ActivityFace;
import com.example.neusoft.yuejian_12.JavaBean.Activity;
import com.example.neusoft.yuejian_12.Listener.ActivityDetailListener;
import com.example.neusoft.yuejian_12.Listener.ActivityListener;
import com.example.neusoft.yuejian_12.Listener.ActivitySignListener;
import com.example.neusoft.yuejian_12.Service.ActivityService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by admin on 2017/9/27.
 */

public class ActivityModel implements ActivityFace {

    private Retrofit retrofit;
    private ActivityService activityService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址*/

    public ActivityModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取活动列表
    @Override
    public void getList(String id, String activity_name,final ActivityListener listListener) {
        activityService = retrofit.create(ActivityService.class);
        Call<List<Activity>> call =activityService.getactivitylist();
        call.enqueue(new Callback<List<Activity>>() {
            @Override
            public void onResponse(Call<List<Activity>> call, Response<List<Activity>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Activity>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    //获取活动详情
    @Override
    public void getdetail(String id,final ActivityDetailListener listListener) {
        activityService = retrofit.create(ActivityService.class);
        Call<Activity> call =activityService.getActivityById(id);
        call.enqueue(new Callback<Activity>() {
            @Override
            public void onResponse(Call<Activity> call, Response<Activity> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Activity> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void signActivity(String actname,String act_id,String telephone,String Anum,String Snum,String Pay_type,
                          String pay,String notice,int userid, final ActivitySignListener listListener) {
       activityService = retrofit.create(ActivityService.class);
        Call<Integer> call =activityService.signActivity("signactivity",actname,act_id,telephone,Anum,Snum,Pay_type,pay,notice,"活动",userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()!=0){
                    listListener.onResponse(response.body());
                }
                else{
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


}


